package com.eduplatform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eduplatform.common.Result;
import com.eduplatform.dto.StudentDTO;
import com.eduplatform.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生管理控制器
 */
@RestController
@RequestMapping("/api/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 分页查询学生列表
     */
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) Integer educationLevel,
            @RequestParam(required = false) String keyword) {
        IPage<StudentDTO> pageResult = studentService.pageStudents(page, size, schoolId, educationLevel, keyword);

        Map<String, Object> data = new HashMap<>();
        data.put("total", pageResult.getTotal());
        data.put("pages", pageResult.getPages());
        data.put("current", pageResult.getCurrent());
        data.put("records", pageResult.getRecords());

        return Result.success(data);
    }

    /**
     * 获取学生详情
     */
    @GetMapping("/{id}")
    public Result getStudentDetail(@PathVariable Long id) {
        StudentDTO student = studentService.getStudentDetail(id);
        if (student == null) {
            return Result.error("学生不存在");
        }
        return Result.success(student);
    }

    /**
     * 添加学生
     */
    @PostMapping("/add")
    public Result addStudent(@RequestBody @Valid StudentDTO studentDTO) {
        try {
            boolean result = studentService.addStudent(studentDTO);
            return result ? Result.success() : Result.error("添加学生失败");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新学生
     */
    @PutMapping("/update")
    public Result updateStudent(@RequestBody @Valid StudentDTO studentDTO) {
        if (studentDTO.getId() == null) {
            return Result.error("学生ID不能为空");
        }

        try {
            boolean result = studentService.updateStudent(studentDTO);
            return result ? Result.success() : Result.error("更新学生失败");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除学生
     */
    @DeleteMapping("/{id}")
    public Result deleteStudent(@PathVariable Long id) {
        try {
            boolean result = studentService.deleteStudent(id);
            return result ? Result.success() : Result.error("删除学生失败");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 批量导入学生
     */
    @PostMapping("/import")
    public Result batchImportStudents(@RequestBody List<StudentDTO> students) {
        try {
            boolean result = studentService.batchImportStudents(students);
            return result ? Result.success() : Result.error("批量导入学生失败");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取学校下的所有学生
     */
    @GetMapping("/school/{schoolId}")
    public Result getStudentsBySchoolId(@PathVariable Long schoolId) {
        List<StudentDTO> students = studentService.getStudentsBySchoolId(schoolId);
        return Result.success(students);
    }

    /**
     * 更新学生状态
     */
    @PostMapping("/{id}/status/{status}")
    public Result updateStudentStatus(@PathVariable Long id, @PathVariable Integer status) {
        try {
            boolean result = studentService.updateStudentStatus(id, status);
            return result ? Result.success() : Result.error("更新学生状态失败");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 重置学生密码
     */
    @PostMapping("/{id}/reset-password")
    public Result resetStudentPassword(@PathVariable Long id) {
        try {
            boolean result = studentService.resetStudentPassword(id);
            return result ? Result.success() : Result.error("重置密码失败");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}